<?php 

namespace app\common\shop;

use app\common\entity\Mywallet;
use app\common\entity\MywalletLog;
use think\Db;
use think\Model;
use traits\model\SoftDelete;


class OrderModel extends model {
    //use SoftDelete;//开启了软删除
    //protected  = 'del_time';
    // 完整数据表名
    protected $table = 'shop_order';


    /**
     * [建表]
     * @return
     */
    public static function createTable(){
        return Db::query("CREATE TABLE If Not Exists `shop_order` (
             `id` int(11) NOT NULL AUTO_INCREMENT,
             `uid` int(11) NOT NULL COMMENT '会员id',
             `paytype` int(11) DEFAULT '0' NOT NULL COMMENT '付款方式',
             `blessnumber` decimal(22,2) NOT NULL DEFAULT '0.00' COMMENT '使用福币数量',
             `shoptype` INT(11) NOT NULL DEFAULT '0' COMMENT '商城类型',
             `total` decimal(22,2) NOT NULL DEFAULT '0.00' COMMENT '总价',
             `blesswait` DECIMAL(22,2) UNSIGNED NOT NULL DEFAULT '0' COMMENT '使用微信支付宝支付时需要的福币',
             `status` int(11) NOT NULL DEFAULT '0' COMMENT '订单状态 0下单 1付款 2待收货 3完成 4取消订单 5退款',
             `createtime` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '时间',
             `ordersn` varchar(255) NOT NULL COMMENT '订单编号',
             `paytime` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '付款时间',
             `sendtime` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '发货时间',
             `finishtime` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '完成时间',
             `expressname` varchar(255) DEFAULT NULL COMMENT '快递公司',
             `expressnumber` varchar(255) DEFAULT NULL COMMENT '快递单号',
             `content` text COMMENT '留言',
             `addressid` int(11) NOT NULL DEFAULT '0' COMMENT '地址id',
             `address_date` text COMMENT '发货之后地址',
             `playgame` INT NOT NULL DEFAULT '0' COMMENT '是否可玩游戏 0是 1否',
             `groupid` int(11) UNSIGNED NOT NULL DEFAULT '0' COMMENT '拼团id',
             `groupstatus` int(11) UNSIGNED NOT NULL DEFAULT '0' COMMENT '拼团状态 0拼团中 1拼团成功',
             PRIMARY KEY (`id`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单表'");
    }

    // 0下单 1付款 2待收货 3完成 4取消订单 5退款	
    public $stata = [
    	0=>'待付款',
    	1=>'待发货',
    	2=>'待收货',
    	3=>'已完成',
    	4=>'取消订单',
        5=>'退款',
    ];
    /*支付方式*/
    public $pay = [
        1=>'微信支付',
        2=>'支付宝支付',
        3=>'代金券支付',
        4=>'福币支付',
        5=>'余额支付',
    ];

    /**
     * [支付方式图片]
     * @return
     */
    public $path=[
        1=>'/pay/weixin.png',
        2=>'/pay/zfb.png',
        3=>'/pay/coupon.png',
        4=>'/pay/bless.png',
        5=>'/pay/money.png',
    ];

 	/**
     * [添加]
     * @return
    */
    public static function add($data){
    	$data['createtime'] = time();
    	return self::insertGetId($data);
    }
    /**
     * [修改]
     * @return
    */
    public static function edit($data,$id){
    	$data['createtime'] = time();
    	return self::where('id',$id)->update($data);
    }

    /**
     * [订单信息]
     * @return
    */
    public static function getInfo($orderid){
    	return self::where('id',$orderid)->find();
    }
    /**
     * [订单信息]
     * @return
     */
    public static function getOrdersnInfo($ordersn){
        return self::where('ordersn',$ordersn)->find();
    }
    /**
     * [订单编号]
     * @return
    */
    public static function getOrdersn(){
    	$ordersn = 'SH'.date('YmdHis').mt_rand(1000,9999);
    	while (1) {
    		$is = self::where('ordersn',$ordersn)->value('id');
    		if(empty($is)) break;
    		$ordersn = 'SH'.date('YmdHis').mt_rand(1000,9999);
    	}
    	return $ordersn;
    }

    /**
     * [付款]
     * $blessnumber 使用福币的数量
     * @return
    */
    public static function payOrder($orderid,$paytype=1,$blessnumber = 0,$order=''){
        // 拼团付款
        ShopGroupModel::pay($orderid);
        if($order['shoptype'] == 4){
            // 代金券直接完成订单
            // return self::where('id',$orderid)->update(['paytime'=>time(),'status'=>3,'paytype'=>$paytype,'finishtime'=>time(),'blessnumber'=>$blessnumber]);

        }else{
        }
            return self::where('id',$orderid)->update(['paytime'=>time(),'status'=>1,'paytype'=>$paytype,'blessnumber'=>$blessnumber]);
    }
    /**
     * [发货]
     * @return
    */
    public static function sendOrder($orderid,$data){
        $data['sendtime'] = time();
        $data['status'] = 2;
        $order = self::getInfo($orderid);
        if($order['credit']>0){
            // 积分大于0，赠送积分
            MywalletLog::addLog($order['uid'], $order['credit'], 5, '购物奖励', 2, 1);
        }
    	return self::where('id',$orderid)->update($data);
    }
    /**
     * [确认收货]
     * @return
    */
    public static function taskOrder($orderid){
        $order = self::where('id',$orderid)->find();
        if($order['shoptype'] == 4){
            // $allmoney = $order['total']+$order['blessnumber'];
            $allmoney = $order['total'];
            MywalletLog::addLog($order['uid'], $allmoney, 2 , '购买代金券', 2, 1);
        }
    	return self::where('id',$orderid)->update(['finishtime'=>time(),'status'=>3]);
    }

    /**
     * [支付方式]
     * @param
     * @return
     */
    public static function getPaytype($goods,$total,$uid = 0){
        $paytype = [];
        if(is_array($goods)){
            $shoptype = $goods['shoptype'];
        }else{
            $shoptype = $goods;
        }
        /*1=>'微信支付',
        2=>'支付宝支付',
        3=>'代金券支付',
        4=>'福币支付',
        5=>'余额支付',*/
        $Mywallet = new Mywallet();
        $bless = $Mywallet->balance($uid,'bless');
        $coupon = $Mywallet->balance($uid,'coupon');
        $money = $Mywallet->balance($uid,'money');
        $class = new OrderModel();
        switch ($shoptype){
            case 1:
                case 3:
                    if($bless >= $total){
                        $paytype = [
                            '0'=>[// 福币
                                'key'=>4,
                                'name'=>$class->pay[4],
                                'pic'=>$class->path[4],
                                'number'=>$bless,
                                'disabled'=>false
                            ],
                            '1'=>[// 余额
                                'key'=>5,
                                'name'=>$class->pay[5],
                                'pic'=>$class->path[5],
                                'number'=>$money,
                                'disabled'=>true
                            ],
                            '2'=>[
                                'key'=>2,
                                'name'=>$class->pay[2],
                                'pic'=>$class->path[2],
                                'disabled'=>false
                            ],
                            '3'=>[
                                'key'=>1,
                                'name'=>$class->pay[1],
                                'pic'=>$class->path[1],
                                'disabled'=>false
                            ],
                        ];
                    }else{
                        $paytype = [
                            '0'=>[// 福币
                                'key'=>4,
                                'name'=>$class->pay[4],
                                'pic'=>$class->path[4],
                                'number'=>$bless,
                                'disabled'=>true
                            ],
                            '1'=>[// 余额
                                'key'=>5,
                                'name'=>$class->pay[5],
                                'pic'=>$class->path[5],
                                'number'=>$money,
                                'disabled'=>false
                            ],
                            '2'=>[
                                'key'=>2,
                                'name'=>$class->pay[2],
                                'pic'=>$class->path[2],
                                'disabled'=>false
                            ],
                            '3'=>[
                                'key'=>1,
                                'name'=>$class->pay[1],
                                'pic'=>$class->path[1],
                                'disabled'=>false
                            ],
                        ];
                    }

                break;
            case 2:
                $paytype = [
                    '0'=>[//代金券
                        'key'=>3,
                        'name'=>$class->pay[3],
                        'pic'=>$class->path[3],
                        'number'=>$coupon,
                        'disabled'=>false
                    ],
                ];
                break;
            case 4:
                if($bless >= $total){
                    $paytype = [
                        '0'=>[// 福币
                            'key'=>4,
                            'name'=>$class->pay[4],
                            'pic'=>$class->path[4],
                            'number'=>$bless,
                            'disabled'=>false
                        ],
                        '1'=>[// 余额
                            'key'=>5,
                            'name'=>$class->pay[5],
                            'pic'=>$class->path[5],
                            'number'=>$money,
                            'disabled'=>true
                        ],
                        '2'=>[
                            'key'=>2,
                            'name'=>$class->pay[2],
                            'pic'=>$class->path[2],
                            'disabled'=>false
                        ],
                        '3'=>[
                            'key'=>1,
                            'name'=>$class->pay[1],
                            'pic'=>$class->path[1],
                            'disabled'=>false
                        ],
                    ];
                }else{
                    $paytype = [
                        '0'=>[// 福币
                            'key'=>4,
                            'name'=>$class->pay[4],
                            'pic'=>$class->path[4],
                            'number'=>$bless,
                            'disabled'=>true
                        ],
                        '1'=>[// 余额
                            'key'=>5,
                            'name'=>$class->pay[5],
                            'pic'=>$class->path[5],
                            'number'=>$money,
                            'disabled'=>false
                        ],
                        '2'=>[
                            'key'=>2,
                            'name'=>$class->pay[2],
                            'pic'=>$class->path[2],
                            'disabled'=>false
                        ],
                        '3'=>[
                            'key'=>1,
                            'name'=>$class->pay[1],
                            'pic'=>$class->path[1],
                            'disabled'=>false
                        ],
                    ];
                }

                break;
        }

        return $paytype;
    }
   
}